/**
 * 
 */
package com.vision.sales.collection.cl.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.i18n.client.HasDirection.Direction;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.form.NumberField;
import com.sencha.gxt.widget.core.client.grid.AggregationNumberSummaryRenderer;
import com.sencha.gxt.widget.core.client.grid.AggregationRowConfig;
import com.sencha.gxt.widget.core.client.grid.AggregationSafeHtmlRenderer;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.GridView;
import com.sencha.gxt.widget.core.client.grid.SummaryType.SumSummaryType;
import com.vision.core.cl.ui.UI;
import com.vision.core.cl.ui.component.BasicGridView;
import com.vision.sales.cl.data.CollectionItemProperties;
import com.vision.sales.cm.db.data.CollectionItem;

/**
 * @author Mark
 *
 */
public class BasicCollectionItemGridPanel extends VerticalLayoutContainer {
	
	ColumnConfig<CollectionItem, String> collectionRefNoCol;
	ColumnConfig<CollectionItem, Date> collectionDateCol;
	ColumnConfig<CollectionItem, Double> grossSalesCol;
	ColumnConfig<CollectionItem, Double> balanceCol;
	
	ColumnConfig<CollectionItem, Double> amountCol;
	
	ColumnConfig<CollectionItem, Double> overrunsCol;
	ColumnConfig<CollectionItem, Double> overpaymentsCol;
	ColumnConfig<CollectionItem, Double> discountsCol;
	ColumnConfig<CollectionItem, Double> rejectsCol;
	ColumnConfig<CollectionItem, Double> returnsCol;
	ColumnConfig<CollectionItem, Double> subtotalCol;
	ColumnConfig<CollectionItem, Double> taxCol;
	ColumnConfig<CollectionItem, Double> netAmountCol;
	
	Grid<CollectionItem> grid;
	
	public BasicCollectionItemGridPanel() {
		initializeComponents();
	}
	
	private void initializeComponents() {
		List<ColumnConfig<CollectionItem, ?>> columns = new ArrayList<ColumnConfig<CollectionItem, ?>>();
		
		CollectionItemProperties props = CollectionItemProperties.INSTANCE;
		
		AbstractCell<Double> decimalCell = UI.decimalCell();
		
		columns.add(UI.column(props.salesInvoiceDate(), 75, "Inv. Date", UI.dateCell()));
		columns.add(collectionRefNoCol = UI.column(props.collectionRefNo(), 85, "Ref. No."));
		columns.add(collectionDateCol = UI.column(props.collectionDate(), 75, "Coll. Date", UI.dateCell()));
		
		columns.add(grossSalesCol = UI.column(props.grossSales(), 80, "Gross Sales", decimalCell, HasHorizontalAlignment.ALIGN_RIGHT));
		columns.add(balanceCol = UI.column(props.balance(), 80, "Balance", decimalCell, HasHorizontalAlignment.ALIGN_RIGHT));
		columns.add(amountCol = UI.column(props.amount(), 80, "Amount", decimalCell, HasHorizontalAlignment.ALIGN_RIGHT));
		columns.add(overrunsCol = UI.column(props.overruns(), 70, "Overruns", decimalCell, HasHorizontalAlignment.ALIGN_RIGHT));
		columns.add(overpaymentsCol = UI.column(props.overpayments(), 70, "Overpayments", decimalCell, HasHorizontalAlignment.ALIGN_RIGHT));
		columns.add(discountsCol = UI.column(props.discounts(), 70, "Discounts", decimalCell, HasHorizontalAlignment.ALIGN_RIGHT));
		columns.add(rejectsCol = UI.column(props.rejects(), 70, "Rejects", decimalCell, HasHorizontalAlignment.ALIGN_RIGHT));
		columns.add(returnsCol = UI.column(props.returns(), 70, "Returns", decimalCell, HasHorizontalAlignment.ALIGN_RIGHT));
		columns.add(subtotalCol = UI.column(props.subtotal(), 80, "Subtotal", decimalCell, HasHorizontalAlignment.ALIGN_RIGHT));
		columns.add(taxCol = UI.column(props.tax(), 55, "Tax", decimalCell, HasHorizontalAlignment.ALIGN_RIGHT));
		columns.add(netAmountCol = UI.column(props.netAmount(), 80, "Net Amount", decimalCell, HasHorizontalAlignment.ALIGN_RIGHT));
		
		for (int i = 2; i < columns.size(); i++) {
			columns.get(i).setAlignment(HorizontalAlignmentConstant.endOf(Direction.LTR));
		}
		
		collectionRefNoCol.setRowHeader(true);
		
		ColumnModel<CollectionItem> cm = new ColumnModel<CollectionItem>(columns);
		
		GridView<CollectionItem> view = new BasicGridView<CollectionItem>();
		view.setEmptyText("There are no collection items to display.");

		grid = new Grid<CollectionItem>(new ListStore<CollectionItem>(props.key()), cm, view);
		grid.setAllowTextSelection(false);
		add(grid, new VerticalLayoutData(1, 1));
		
		// Aggregation
		AggregationRowConfig<CollectionItem> total = new AggregationRowConfig<CollectionItem>();
		total.setRenderer(collectionRefNoCol, new AggregationSafeHtmlRenderer<CollectionItem>(SafeHtmlUtils.fromTrustedString("<b>TOTAL</b>")));
		AggregationNumberSummaryRenderer<CollectionItem, Double> renderer = new AggregationNumberSummaryRenderer<CollectionItem, Double>(UI.AMOUNT_FORMAT, new SumSummaryType<Double>()) {
//			@Override
//			public SafeHtml render(int colIndex, Grid<CollectionItem> grid) {
//				SafeHtml s = super.render(colIndex, grid);
//				return SafeHtmlUtils.fromTrustedString("<b>" + s.asString() + "</b>");
//			}
		};
	    total.setRenderer(amountCol, renderer);
	    total.setRenderer(overpaymentsCol, renderer);
	    total.setRenderer(overrunsCol, renderer);
	    total.setRenderer(discountsCol, renderer);
	    total.setRenderer(rejectsCol, renderer);
	    total.setRenderer(returnsCol, renderer);
	    total.setRenderer(subtotalCol, renderer);
	    total.setRenderer(taxCol, renderer);
	    total.setRenderer(netAmountCol, renderer);
	    cm.addAggregationRow(total);
		
		NumberField<Double> field = new NumberField<Double>(UI.doubleEditor());
		field.setClearValueOnParseError(true);
	}
	
	public ListStore<CollectionItem> getStore()
	{
		return grid.getStore();
	}

}
